﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;


namespace EnblibTest
{
    public partial class Home : Form
    {
        public Home()
        {
            InitializeComponent();
        }
        public void showView()
        {
            //List<Test_User> list = new List<Test_User>();
            //Database db = GetData.CreateDataBase();
            //StringBuilder sb = new StringBuilder();
            //sb.Append("select * from Enblib ");
            //DbCommand cmd = db.GetSqlStringCommand(sb.ToString());
            //using (IDataReader reader = db.ExecuteReader(cmd))
            //{
            //    while (reader.Read())
            //    {
            //        list.Add(new Test_User()
            //        {
            //            No = reader.GetString(0),
            //            Name = reader.GetString(1),
            //            Dept = reader.GetString(2),
            //            Sex = reader.GetString(3)
            //        });
            //    }
            //}
            //dataGridView1.DataSource = list;

            
            Database db = GetData.CreateDataBase();
            IRowMapper<Test_User> mapper = MapBuilder<Test_User>.BuildAllProperties();
            DataAccessor<Test_User> userAccessor;
            userAccessor = db.CreateSqlStringAccessor("select * from Enblib", mapper);
            var list = userAccessor.Execute().ToList();
            dataGridView1.DataSource = list;
            //    list = userAccessor.Execute().ToList();
            //    //ds = db.ExecuteDataSet(CommandType.Text,"select * from Enblib");
            //    dataGridView1.DataSource = list;
            //    //dataGridView1.Columns[0].Visible = false;
        }

        private void Home_Load(object sender, EventArgs e)
        {
            this.showView();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            UserEdit edit = new UserEdit();
            edit.Show(this);
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            /*Database db = GetData.CreateDataBase();
            StringBuilder sb = new StringBuilder();
            sb.Append("update Enblib set id=@id,");
            sb.Append("name=@name,");
            sb.Append("dept=@dept,");
            sb.Append("sex=@sex ");
            sb.Append("where no=@no");
            DbCommand cmd = db.GetSqlStringCommand(sb.ToString());
            db.AddInParameter(cmd, "@id", DbType.String, dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1].EditedFormattedValue);
            db.AddInParameter(cmd, "@name", DbType.String, dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[2].EditedFormattedValue);
            db.AddInParameter(cmd, "@dept", DbType.String, dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[3].EditedFormattedValue);
            db.AddInParameter(cmd, "@sex", DbType.String, dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[4].EditedFormattedValue);
            db.AddInParameter(cmd, "@no", DbType.String, dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[0].Value);
            try
            {
                int count = db.ExecuteNonQuery(cmd);
                if (count > 0)
                {
                    MessageBox.Show("修改成功");
                    this.showView();
                }
                else
                {
                    MessageBox.Show("修改失败");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }*/

            Database db = GetData.CreateDataBase();
            IRowMapper<Test_User> mapper = MapBuilder<Test_User>.BuildAllProperties();
            string no = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[0].Value.ToString();
            var results = db.ExecuteSqlStringAccessor("select * from Enblib where no='" + no + "'", mapper);
            // db.AddInParameter(cmd, "@no", DbType.String, dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[0].Value);
            Test_User testUsr = null;
            foreach (var usr in results)
            {
                testUsr = usr;
            }
            if (testUsr != null)
            {
                UserEdit edit = new UserEdit(testUsr);
                if (edit.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                {
                    this.showView();
                }
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            // var list = new List<Test_User>();
            //Test_User delUser = new Test_User();
            //delUser.No = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[0].Value.ToString();
            //Database db = GetData.CreateDataBase();
            //StringBuilder sb = new StringBuilder();
            //sb.Append("delete from Enblib ");
            //sb.Append(" where No=@No");
            //DbCommand cmd = db.GetSqlStringCommand(sb.ToString());
            //db.AddInParameter(cmd, "@No", DbType.String, delUser.No);
            //if (db.ExecuteNonQuery(cmd) > 0 ? true : false)
            //{
            //    MessageBox.Show("删除成功");
            //    this.showView();
            //}
            //else { MessageBox.Show("删除失败"); }

            Database db = GetData.CreateDataBase();
            DataAccessor<Test_User> userAccessor;
            IRowMapper<Test_User> mapper = MapBuilder<Test_User>.BuildAllProperties();
            string no = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[0].Value.ToString();
            userAccessor = db.CreateSqlStringAccessor("delete from Enblib where no='" + no + "'", mapper);
            var list = userAccessor.Execute().ToList();
            if (list.Count == 0)
            {
                MessageBox.Show("删除成功");
                this.showView();
            }
            else
            {
                MessageBox.Show("删除失败");
            }

            //Database db = GetData.CreateDataBase();
            //Test_User testUsr = new Test_User();
            //StringBuilder sb = new StringBuilder();
            //sb.Append("delete from Enblib ");
            //sb.Append("where no=@no");
            //DbCommand cmd = 
            //db.AddInParameter(cmd,"@no",DbType.String,testUsr.No);
            //int count = db.ExecuteNonQuery(cmd);
            //if (count > 0)
            //{
            //    MessageBox.Show("删除成功");
            //    this.showView();
            //}
            //else
            //{ MessageBox.Show("删除失败"); }
            //sb.Append("delete from Enblib where id=");
            //sb.Append(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1].Value);
            //DbCommand cmd = db.GetSqlStringCommand(sb.ToString());
            //int count = db.ExecuteNonQuery(cmd);
            //if (count > 0)
            //{
            //    MessageBox.Show("删除成功");
            //    this.showView();
            //}
            //else
            //{ MessageBox.Show("删除失败"); }

        }
    }
}
